package main

import (
	"gitee.com/lintx/genshin-impact-helper-go/config"
	"gitee.com/lintx/genshin-impact-helper-go/logger"
	"gitee.com/lintx/genshin-impact-helper-go/sign/mys"
	"github.com/robfig/cron/v3"
	"sync"
	"time"
)

var day = -1
var m *mys.Mys
var lock sync.Mutex

func main() {
	logger.InitLogger()
	defer logger.Sync()
	config.InitConfig()
	cr := cron.New(cron.WithSeconds())
	for _, mc := range config.C.MysConfig {
		t := mc.Cron
		if t == "" {
			t = config.C.DefaultCron
		}
		if t == "" {
			continue
		}
		f := getFunc(mc)
		f()
		_, _ = cr.AddFunc(t, f)
	}
	cr.Run()
	select {}
}

func getFunc(c config.Mys) func() {
	return func() {
		lock.Lock()
		defer lock.Unlock()
		n := time.Now().Day()
		if n != day {
			m = mys.New()
			day = n
		}
		logger.Logger.Info(`开始签到`)
		m.Sign(c.Cookie)
	}
}
